import {request} from '../../utils/http'
import {upload} from '../../utils/upload';
import {requestUrl} from '/config';

Page({
  data: {
    order_id: 0,
    apply_note: '',
    imgList:[],
    apply_images:[],
    success: false,
    action: requestUrl + '/upload/file'
  },
  onLoad(query) {
    this.setData({
      order_id: query.order_id,
    })
  },
  uploadImage(){
    let self = this;
    my.chooseImage({
      sourceType: [ 'album'],
      count: 1,
      success: (res) => {
        console.log(res)
        self.upload(res.apFilePaths[0])
      },
      fail: (res) => {
        // 可自行查看错误信息并进行相关处理
        console.log(res);
      }
    })
  },
  upload(path){
    upload(path).then(res => {
      console.log(res)
    })
  },
  bindKeyInputName(e) {
    this.setData({
      apply_note: e.detail.value
    })
  },
  save() {
    if (!this.data.apply_note) {
      my.showToast({content: '请输入原因'})
      return
    }
    request('/order/breakApply', 'post', {
      order_id: this.data.order_id,
      apply_note: this.data.apply_note,
      apply_images: this.data.apply_images
    }).then(res => {
      if (res.code == 200) {
        this.setData({
          success: true
        })
      } else {
        my.showToast({content: res.message});

      }
     
    })
  },
  toOrderDetail() {
    my.navigateTo({
      url: '../orderDetail/index?id=' + this.data.order_id
    });
  },
  toHome() {
    my.switchTab({url: '../index/index'})
  },
  onDelete(e){
    console.log(e)
  },
  onUploadChange(list){
    console.log(list)
    let apply_images = [];
    list.forEach(item => {
      if (item.url) {
        apply_images.push(item.url);

      }
    });
    this.setData({
      apply_images
    })
  },
  onAfterUpload(res){
    return new Promise((resolve) => {
      if (res.code == 200) {
        if (res.data.url) {
          resolve(res.data.url)
        }
      } else {
        resolve('')
      }
    })
  }
})